import { returnItems, fishItems, unitItems } from "../../config/index";
import { isNumber } from "../../utils/check";
import {
  createFishingReturn,
  updateFishingReturn,
  deleteFishingReturn,
} from "../../api/fishing-return";

const defalutData = {
  mode: returnItems[0].value,
  fish: "",
  price: "",
  unit: "斤",
};

Component({
  properties: {
    spotId: { type: String, value: "" },
    editable: { type: Boolean, value: false },
    arrayData: { type: Array, value: [] },
  },

  data: {
    selfEditable: false,
    fishItems,
    returnItems,
    unitItems,
    popupVisible: false,
    editingIndex: -1, // 当前编辑的索引, -1表示新增
    formData: {
      ...defalutData,
    },
  },
  methods: {
    onChangeEditMode() {
      this.setData({
        selfEditable: !this.data.selfEditable,
      });
    },

    // 显示新增弹框
    onAdd() {
      this.setData({
        editingIndex: -1,
        popupVisible: true,
        formData: {
          ...defalutData,
        },
      });
    },

    // 输入框
    onInputChange(e) {
      const { feild } = e.currentTarget.dataset;
      console.log("---feild------", feild, e.detail.value);
      this.setData({
        [`${feild}`]: e.detail.value,
      });
    },

    // 显示编辑弹框
    onEdit(e) {
      if (!this.data.selfEditable && !this.properties.editable) {
        return;
      }
      const { index } = e.currentTarget.dataset;
      const targetData = this.data.arrayData[index];
      console.log("---onEdit------", index, targetData);
      this.setData({
        editingIndex: index,
        formData: { ...targetData },
        popupVisible: true,
      });
    },

    // 请求删除
    onDelete(e) {
      const { index } = e.currentTarget.dataset;
      wx.showModal({
        title: "确认删除",
        content: "您确定要删除这条回鱼信息吗？",
        success: (res) => {
          if (res.confirm) {
            this.triggerEvent("delete", { index });
          }
        },
      });
    },

    // 保存
    async onSubmit() {
      const { editingIndex } = this.data;
      const data = this.validateForm();

      if (!data) {
        return;
      }

      if (this.data.selfEditable) {
        this.innerSave(data, editingIndex);
        return;
      }
      this.triggerEvent("save", {
        index: editingIndex,
        data,
      });
      this.onCancel();
    },

    // 内部保存
    async innerSave(data, editingIndex) {
      wx.showLoading({ title: "保存中..." });
      // console.log("----data----", data);
      const id = this.data.formData.id;
      let res;
      if (id) {
        res = await updateFishingReturn(id, data);
      } else {
        data.sessionId = this.properties.spotId;
        res = await createFishingReturn(data);
      }
      // console.log("----res----", res);
      wx.hideLoading();
      if (res.error) {
        wx.showToast("保存失败");
        return;
      }
      wx.showToast({
        title: "保存成功",
      });
      // 通知上层改数据
      this.triggerEvent("save", {
        index: editingIndex,
        data,
      });
      this.onCancel();
    },

    // 取消
    onCancel() {
      this.setData({ popupVisible: false });
    },

    validateForm() {
      const { formData } = this.data;
      const array = [
        { key: "mode", title: "请选择回鱼类型" },
        { key: "fish", title: "请选择回鱼种类" },
        { key: "unit", title: "请选择回鱼方式" },
        { key: "price", title: "请输入回鱼价格" },
      ];
      const data = {};
      for (let index = 0; index < array.length; index++) {
        const item = array[index];
        const { key, title } = item;
        if (!formData[key] || formData[key].trim() === "") {
          wx.showToast({
            title,
            icon: "none",
          });
          return false;
        }
        data[key] = formData[key].trim();
      }
      if (!isNumber(formData.price)) {
        wx.showToast({
          title: "回鱼价格,请输入0-999间的整数",
          icon: "none",
        });
        return false;
      }
      data.price = Number(data.price);

      return data;
    },
  },
});
